package bdeenyy.threevector;

import bdeenyy.threevector.controller.DataTabController;
import bdeenyy.threevector.controller.ResultTabController;
import javafx.application.Application;
import javafx.geometry.Insets;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Tab;
import javafx.scene.control.TabPane;
import javafx.scene.layout.HBox;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;

import java.util.Objects;

public class Main extends Application {
    private TabPane tabPane;
    private static boolean isDataTabClosed = false;
    private static boolean isResultTabClosed = false;
    private DataTabController dataTabController;
    private ResultTabController resultTabController;

    public static void main(String[] args) {
        launch(args);
    }

    @Override
    public void start(Stage primaryStage) {
        resultTabController = new ResultTabController();
        dataTabController = new DataTabController(resultTabController);

        tabPane = new TabPane();
        Tab dataTab = dataTabController.createDataTab();
        Tab resultTab = resultTabController.createResultTab();

        tabPane.getTabs().addAll(dataTab, resultTab);

        Button reopenDataTabButton = new Button("Открыть данные");
        Button reopenResultTabButton = new Button("Открыть результат");

        reopenDataTabButton.setOnAction(event -> reopenTab("Данные"));
        reopenResultTabButton.setOnAction(event -> reopenTab("Результат"));

        HBox buttonBox = new HBox(10, reopenDataTabButton, reopenResultTabButton);
        VBox root = new VBox(10, tabPane, buttonBox);
        root.setPadding(new Insets(10));

        Scene scene = new Scene(root, 1200, 600);

        // Применение стиля из CSS
        scene.getStylesheets().add(Objects.requireNonNull(getClass().getResource("/styles.css")).toExternalForm());

        primaryStage.setTitle("Vector Calculator");
        primaryStage.setScene(scene);
        primaryStage.show();
    }

    private void reopenTab(String tabName) {
        if (tabName.equals("Данные") && isDataTabClosed) {
            Tab dataTab = dataTabController.createDataTab();
            tabPane.getTabs().add(dataTab);
            isDataTabClosed = false;
        } else if (tabName.equals("Результат") && isResultTabClosed) {
            Tab resultTab = resultTabController.createResultTab();
            tabPane.getTabs().add(resultTab);
            isResultTabClosed = false;
        }
    }

    public static void setDataTabClosed(boolean closed) {
        isDataTabClosed = closed;
    }

    public static void setResultTabClosed(boolean closed) {
        isResultTabClosed = closed;
    }
}
